package com.jboost.modules.file.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jboost.common.constant.CommonConstant;
import com.jboost.common.util.oss.OssBootUtil;
import com.jboost.modules.file.config.FileProperties;
import com.jboost.modules.file.entity.JboostFile;

public class FileUtils {

    //拼接文件预览URL地址
    public static String getFileUrl(JboostFile file) {
        String webHost = FileProperties.WEB_HOST;
        String port = FileProperties.PORT;
        String context = FileProperties.CONTEXT;

        String store = file.getFileStore();
        JSONObject storeJson = JSON.parseObject(store);
        String saveType = storeJson.getString("type");
        String savePath = storeJson.getString("path");
        if (CommonConstant.UPLOAD_TYPE_LOCAL.equals(saveType)) {
            return (port == null || "80".equals(port)?"http://":"https://") + webHost + (port == null || ("80".equals(port)||"443".equals(port)) ? "" : (":" + port)) + context + "/sys/common/static/" + savePath+"?type=review";
        } else if(CommonConstant.UPLOAD_TYPE_OSS.equals(saveType)) {
            return OssBootUtil.getFullUrl(savePath,null);
        }else {
            return savePath;
        }
    }
    //拼接文件下载URL地址
    public static String getFileDownUrl(JboostFile file) {
        String webHost = FileProperties.WEB_HOST;
        String port = FileProperties.PORT;
        String context = FileProperties.CONTEXT;

        String store = file.getFileStore();
        JSONObject storeJson = JSON.parseObject(store);
        String saveType = storeJson.getString("type");
        String savePath = storeJson.getString("path");
        if (CommonConstant.UPLOAD_TYPE_LOCAL.equals(saveType)) {
            return (port == null || "80".equals(port)?"http://":"https://") + webHost + (port == null || ("80".equals(port)||"443".equals(port)) ? "" : (":" + port)) + context + "/sys/common/static/" + savePath+"?type=download";
        } else if(CommonConstant.UPLOAD_TYPE_OSS.equals(saveType)) {
            return OssBootUtil.getDowntUrl(savePath,null);
        }else {
            return savePath;
        }
    }

}
